import JSONBig from 'json-bigint'
const baseUrl = 'https://api.daccf.com';
// const baseUrl = 'http://192.168.31.47:7001';
export function request(options) {
	const userConfig = uni.getStorageSync('userConfig')
	// if(userConfig){
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + options.url,
				data: options.data || {},
				method: options.method,
				header: {
					"testToken": "2gq72h2qrbhx256y0167uf5wd64ls55u",
					"BGDEBUG": 0,
					"Authorization": uni.getStorageSync('userInfo').accessToken || "",
					"Content-Type": "application/x-www-form-urlencoded",
					"ADMIN_ID": userConfig.adminId,
					"USER_SERVICE_TOKEN":userConfig.userServiceToken,
					"clientId": uni.getStorageSync('__DC_STAT_UUID') || "",
				},
				dataType: 'string',
				success(res) {
					// console.log(res,'99999999999',options.url,options.data);
					let data =JSONBig.parse(res.data) ;
					
					if(data.code==200){
						resolve(data);
					}else if(data.code==11001){
						uni.showToast({
							icon:'none',
							title:'登陆过期，请重新登陆'
						})
						setTimeout(()=>{
							uni.navigateTo({
								url:'/pages/user/login'
							})
						},1000)
					}else{
						uni.showToast({
							icon:'none',
							title:data.msg
						})
					}
		
				},
				fail(res) {
					console.log(res,'99999999999',options.url,options.data);
					uni.showToast({
						icon:'none',
						title:data.code.msg
					})
					// 失败处理
					reject(res);
				}
			});
		});
	// }else{
		
	// }
	
}

export function server(options) {
	const userConfig = uni.getStorageSync('userConfig')
	return new Promise((resolve, reject) => {
		
		uni.request({
			url: baseUrl + options.url,
			data: options.data || {},
			method: options.method,
			header: {
				"testToken": "2gq72h2qrbhx256y0167uf5wd64ls55u",
				"BGDEBUG": 0,
				"Authorization": uni.getStorageSync('userInfo').accessToken || "",
				"Content-Type": "application/json",
				"ADMIN_ID": userConfig.adminId,
				"USER_SERVICE_TOKEN":userConfig.userServiceToken,
				"clientId": uni.getStorageSync('__DC_STAT_UUID') || "",
			},
			dataType: 'string',
			success(res) {
				let data =JSONBig.parse(res.data) ;
				if(data.code==200){
					resolve(data);
				}else if(data.code==11001){
					uni.showToast({
						icon:'none',
						title:'登陆过期，请重新登陆'
					})
					setTimeout(()=>{
						uni.navigateTo({
							url:'/pages/user/login'
						})
					},1000)
				}else{
					uni.showToast({
						icon:'none',
						title:data.msg
					})
				}

			},
			fail(res) {
				// 失败处理
				reject(res);
			}
		});
	});
}

export function serverUpload(options) {
	const userConfig = uni.getStorageSync('userConfig')
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: baseUrl + options.url,
			formData: options.data || {},
			method: options.method,
			header: {
				"testToken": "2gq72h2qrbhx256y0167uf5wd64ls55u",
				"BGDEBUG": 0,
				"Authorization": uni.getStorageSync('userInfo').accessToken || "",
				"Content-Type": "application/json",
				"ADMIN_ID": userConfig.adminId,
				"USER_SERVICE_TOKEN":userConfig.userServiceToken,
				"clientId": uni.getStorageSync('__DC_STAT_UUID') || "",
			},
			filePath: options.data.file,
			name: 'file',
			dataType: 'string',
			success(res) {
				let data =JSONBig.parse(res.data) ;
				if(data.code==200){
					resolve(data);
				}else if(data.code==11001){
					uni.showToast({
						icon:'none',
						title:'登陆过期，请重新登陆'
					})
					setTimeout(()=>{
						uni.navigateTo({
							url:'/pages/user/login'
						})
					},1000)
				}else{
					uni.showToast({
						icon:'none',
						title:data.msg
					})
				}
			},
			fail(res) {
				// 失败处理
				reject(res);
			}
		});
	});
}

export function https(options){
	console.log(options);
        return new Promise((resolve, reject) => {
                uni.request({
                        url: options.url,
                        data: options.data || {},
                        method: options.method,
                        header: {
                                "Content-Type": "application/json",
                        },
                        success(res) {
							console.log(res,778888);
                                resolve(res.data)
                        },
						fail() {
							console.log(99999);
						}

                });
        });
}



